package com.example.qimowork.adapters;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.qimowork.R;
import com.example.qimowork.ShuJu.Title;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {
    private List<Title> titleList;
    private Context context;
    private int layoutPosition;

    private OnItemClickListener onItemClickListener = null;
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
    public MyRecyclerViewAdapter(List<Title> arrList, Context context) {
        this.titleList = arrList;
        this.context = context;
    }
   @NonNull
   @Override
   public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
       View inflate = LayoutInflater.from(context).inflate(R.layout.shouye_item, parent,false);
       ViewHolder holder = new ViewHolder(inflate);
       return holder;
   }

   @Override
   public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
       holder.bt.setText(titleList.get(position).getTitleName());
       holder.itemView.setTag(position);
       holder.itemView.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               if (onItemClickListener != null) {
                   layoutPosition = holder.getLayoutPosition();
                   notifyDataSetChanged();
                   onItemClickListener.onItemClick(holder.itemView, (int) holder.itemView.getTag());
               }
           }
       });
       if (position == layoutPosition) {
           holder.bt.setTextColor(Color.parseColor("#ff0000"));
       } else {
           holder.bt.setTextColor(Color.parseColor("#000000"));
       }
   }

   @Override
   public int getItemCount() {
       return titleList != null ? titleList.size():0;
   }

   public class ViewHolder extends RecyclerView.ViewHolder{
       TextView bt;
       public ViewHolder(@NonNull View itemView) {
           super(itemView);
           bt = itemView.findViewById(R.id.shouye_iten_tv);
       }
   }

    public void setBg(int pos){
        this.layoutPosition = pos;
        notifyDataSetChanged();
    }
}
